package peer;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class PeerUI extends JFrame {

	private JTextArea logging;	

	public static void main(String args[]) {

		PeerUI ui = new PeerUI();
		ui.setVisible(true);
	}

	public PeerUI() {
		initUi();
		new Peer(new Neighbor("127.0.0.1", 5000));
	}

	private void initUi() {

		this.setSize(500, 300);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		initTabbed();
		initLogging();
	}

	private void initTabbed() {

		JTabbedPane tabs = new JTabbedPane();

		JComponent mainTab = initMainTab(new JPanel());
		JComponent addTracker = initTrackerInfo(new JPanel());
		JComponent p2pCommand = initCommand(new JPanel());
		JComponent neighbors = initNeighbors(new JPanel());

		
		tabs.addTab("Main Tab", null, mainTab, "Tracker - Info");
		
		tabs.addTab("Tracker - Info", null, addTracker, "Tracker - Info");

		tabs.addTab("p2p - Command", null, p2pCommand, "Tracker - Info");

		tabs.addTab("Neighbors - Info", null, neighbors, "Neighbors - Info");

		this.getContentPane().add(tabs);

	}

	private JComponent initMainTab(JComponent trackerInfo) {
		
		JButton start = new JButton("Start Program");
		start.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO
				logPeerAction("Start Program clicked!");
			}
		});
		
		
		JButton clear = new JButton("Clear");
		clear.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO
				logging.setText("");
			}
		});


		trackerInfo.add(start);
		trackerInfo.add(clear);
		return trackerInfo;
	}

	private JComponent initTrackerInfo(JComponent trackerInfo) {

		final JTextField ip = new JTextField("127.0.0.1", 20);
		JLabel ipLabel = new JLabel("Tracker Ip");
		JButton connect = new JButton("Connect");
		connect.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO
				logPeerAction("Connect clicked!");
			}
		});

		trackerInfo.add(ipLabel);
		trackerInfo.add(ip);
		trackerInfo.add(connect);

		return trackerInfo;

	}

	private JComponent initCommand(JComponent command) {
		JButton spawn = new JButton("Spawn");
		final JTextField ip = new JTextField("127.0.0.1", 20);

		spawn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO
				logPeerAction("Spawn clicked!");
			}
		});

		JButton leave = new JButton("Leave");
		leave.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO
				logPeerAction("Leave clicked!");
			}
		});

		command.add(ip);
		command.add(spawn);
		command.add(leave);
		return command;

	}

	private JComponent initNeighbors(JComponent neighbors) {

		return neighbors;
	}

	private void initLogging() {

		logging = new JTextArea(12, this.getContentPane().getWidth());

		logging.setEditable(true);

		logging.setBackground(Color.white);

		JScrollPane scrollPane = new JScrollPane(logging);
		this.getContentPane().add(scrollPane, BorderLayout.SOUTH);

	}

	public void logPeerAction(String action) {

		String current = logging.getText();
		current += action + "\n";
		this.logging.setText(current);

	}

}
